package com.longrise.common.base.web;

import android.graphics.Bitmap;
import android.util.Log;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.longrise.common.utils.PrintLog;
import com.longrise.umeng.statistics.UMHybrid;
import com.umeng.analytics.MobclickAgent;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by callxkj on 2017/10/18.
 * BaseWebClient
 */

public class BaseWebClient extends WebViewClient {


    private static final String TAG = "BaseWebClient";
    public final BaseWebActivity mActivity;

    public BaseWebClient(BaseWebActivity activity) {
        this.mActivity = activity;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (mActivity.checkNetWorkEnable()) {

            //------友盟统计---start-------
            try {
                String decodedURL = java.net.URLDecoder.decode(url, "UTF-8");
                UMHybrid.getInstance(mActivity).execute(decodedURL, view);
            } catch (Exception e) {
                e.printStackTrace();
            }

            //------友盟统计---end---------
        } else {
            mActivity.showNonNetPage();
        }
        return false;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        mActivity.mUrl = url;
        if (mActivity.checkNetWorkEnable()) {
            mActivity.onLoading();
        } else {
            mActivity.showNonNetPage();
        }
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        //延时300毫秒 以防闪烁
        Observable.timer(300, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {

                    private Disposable mD;

                    @Override
                    public void onSubscribe(Disposable d) {
                        mD = d;
                    }

                    @Override
                    public void onNext(Long value) {
                        mActivity.loadSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mActivity.showNonNetPage();
                    }

                    @Override
                    public void onComplete() {
                        if (mD != null) {
                            mD.dispose();
                        }
                    }
                });

    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        mActivity.showNonNetPage();
    }

}
